# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=lua-xctrl
pkgver=2015.04.10
pkgrel=4
pkgdesc="Lua module that allows you to query and manipulate various aspects of an X11 window manager"
url="https://github.com/yetanothergeek/xctrl"
arch="all"
license="GPL-2.0-or-later"
makedepends="lua-dev libx11-dev libxmu-dev"
subpackages="$pkgname-doc"
source="$pkgname-$pkgver.tar.gz::https://github.com/yetanothergeek/xctrl/archive/$pkgver.tar.gz
	fix-linking.patch
	"
builddir="$srcdir"/xctrl-$pkgver

# higher versions fail to import (lua_pcall not found)
_luaversions="5.1"
for _i in $_luaversions; do
	makedepends="$makedepends lua$_i-dev"
	subpackages="$subpackages lua$_i-xctrl:_subpackage"
done

prepare() {
	default_prepare
	local lver; for lver in $_luaversions; do
		cp -r "$builddir" "$builddir-$lver"
	done
}

build() {
	local lver; for lver in $_luaversions; do
		msg "building for Lua $lver..."
		cd "$builddir-$lver"
		make EXTRA_CFLAGS="$CFLAGS -fPIC" LUA="lua$lver"
	done
}

check() {
	local lver; for lver in $_luaversions; do
		msg "Checking lua $lver build"
		(
		cd "$builddir-$lver"/src
		# check if importable
		lua$lver -e 'require("xctrl")'
		)
	done
}

package() {
	local lver; for lver in $_luaversions; do
		install -Dm644 "$builddir-$lver"/src/xctrl.so \
			-t "$pkgdir"/usr/lib/lua/$lver
		install -Dm644 doc/lxctrl.html \
			-t "$pkgdir"/usr/share/doc/$pkgname
	done
}

_subpackage() {
	local lver="${subpkgname:3:3}"
	pkgdesc="$pkgdesc $lver"
	depends="lua$lver"

	amove usr/lib/lua/$lver
}

sha512sums="
11a48641ebc71ab56201a707b844ee0d0475167fe4f4bd96f4d8296db00dc806c6d3889a5447bb41cec787b0de6d0351f77b45a8505fac5a5e184e3b80482acb  lua-xctrl-2015.04.10.tar.gz
37707f5a5a697eed1f3776fd33dc5140d0b0480ec39503d79f2793cb338fa28f68740b3f80443d957cfeeaca2593036e8b22a47b89bd445826b7515463489320  fix-linking.patch
"
